import { request } from '../../request/index'
import regeneratorRuntime from "../../lib/runtime/runtime"
Page({
  data: {
    goodsDeail: {}
  },
  goodsInfo: {},
  /*监听页面加载*/
  onLoad: function (options) {
    const { goods_id } = options;
    this.getGoodsDetail(goods_id);
  },
  async getGoodsDetail(goods_id) {
    const res = await request({
      data: { goods_id },
      url: '/goods/detail'
    })
    this.goodsInfo = res.data.message;
    this.setData({
      goodsDeail: {
        pics: res.data.message.pics,
        goods_name: res.data.message.goods_name,
        goods_price: res.data.message.goods_price,
        goods_introduce: res.data.message.goods_introduce,
      }
    });
  },
  // 图片预览
  previewImage(e) {
    const current = e.currentTarget.dataset.url;
    const urls = this.goodsInfo.pics.map(v => v.pics_mid);
    wx.previewImage({
      current, // 当前显示图片的http链接
      urls // 需要预览的图片http链接列表
    })
  },
  // 点击 加入购物车
  addCart() {
    let cart = wx.getStorageSync('cart') || [];
    let index = cart.findIndex(v => v.goods_id === this.goodsInfo.goods_id);
    if(index === -1) {
      this.goodsInfo.num = 1;
      this.goodsInfo.checked = true;
      cart.push(this.goodsInfo);
    } else {
      cart[index].num++;
    }
    wx.setStorageSync('cart', cart);
    wx.showToast({
      icon: 'success',
      title: '添加成功',
      mask: true, // 防止用户手抖, 疯狂点击按钮
    })
  }
})